package com.qianxun.servers;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import com.qianxun.iservers.IServer;
import com.qianxun.model.ServerCfg;

public class MyServer implements IServer,Serializable{

	public MyServer(){
		this.serverList.add(myServerCfg);
	}
	public boolean register(ServerCfg server) {
		System.out.println(server.getHostPort());
		if(server!=null){
			this.serverList.add(server);
			for(ServerCfg s:this.serverList){
				System.out.println(s.getName());
			}			
			return true;
		}
		return false;
	}

	public boolean isActive(ServerCfg server) {
		// TODO Auto-generated method stub
		
		return false;
	}

	public boolean exit(ServerCfg server) {
		return this.serverList.remove(server);
	}

	public ArrayList getData(String query) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getSingleData(String query) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean updateServerList() {
	
		for(ServerCfg s:this.serverList){
			try {
				IServer otherServer = (IServer)Naming.lookup(s.getName());
				otherServer.updateServerList(this.serverList);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			
		}
		return true;
	}
	public boolean updateServerList(ArrayList<ServerCfg> servers) {
		return false;
	}
	public boolean exit() {
		// TODO Auto-generated method stub
		return false;
	}
	
}
